package com.zl.autocode.service.autocode.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zl.autocode.common.constant.AutocodeEnum;
import com.zl.autocode.dao.autocode.ZlKeywordDtlMapper;
import com.zl.autocode.dao.autocode.ZlKeywordMapper;
import com.zl.autocode.dto.autocode.keywordDtl.ReqKeywordDtlAddDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ReqKeywordDtlDelDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ReqKeywordDtlDetailDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ReqKeywordDtlEditDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ReqKeywordDtlQueryDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ReqKeywordDtlStateDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ResKeywordDtlAddDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ResKeywordDtlDelDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ResKeywordDtlDetailDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ResKeywordDtlEditDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ResKeywordDtlQueryDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ResKeywordDtlStateDTO;
import com.zl.autocode.dto.autocode.keywordDtl.ResKeywordDtlVO2;
import com.zl.autocode.entity.autocode.ZlKeyword;
import com.zl.autocode.entity.autocode.ZlKeywordDtl;
import com.zl.autocode.entity.autocode.po.ZlKeywordDtlVo;
import com.zl.autocode.service.autocode.IKeywordDtlService;
import com.zl.autocode.service.base.AutocodeBaseManager;
import com.zl.common.dto.ReqDTO;
import com.zl.common.dto.pagination.PageReqVO;
import com.zl.common.enums.resp.GlobalRespEnum;
import com.zl.common.exception.BizException;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;

/**
 * 数据库关键字明细表
 * @author ZL
 */
@Service("autocodeKeywordDtlService")
public class KeywordDtlServiceImpl extends AutocodeBaseManager implements IKeywordDtlService {

    @Autowired
    private ZlKeywordDtlMapper keywordDtlMapper;
    @Autowired
    private ZlKeywordMapper keywordMapper;

    @Override
    public ResKeywordDtlQueryDTO queryKeywordDtlList(ReqDTO<ReqKeywordDtlQueryDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqKeywordDtlQueryDTO reqData = reqInfo.getReqData();
        PageReqVO page = pageHandle(reqInfo.getPageInfo());//处理分页数据
        ResKeywordDtlQueryDTO resDTO = BeanUtil.copyProperties(page, ResKeywordDtlQueryDTO.class);
        if(StringUtils.isNotBlank(reqData.getKeywordVal())){
            reqData.setKeywordVal("%"+reqData.getKeywordVal()+"%");
        }
        Map<String,Object> paramMap = BeanUtil.beanToMap(reqData,false,true);
        
        Page<Object> pageHp = PageHelper.startPage(page.getPage(), page.getLimit(),page.getSortorder());
        List<ZlKeywordDtlVo> list = keywordDtlMapper.selectListPage(paramMap);
        resDTO.setData(BeanUtil.copyToList(list, ResKeywordDtlVO2.class));
        resDTO.setCount(pageHp.getTotal());
        
        return resDTO;
    }
    
    @Override
    public ResKeywordDtlDetailDTO queryKeywordDtlDetail(ReqDTO<ReqKeywordDtlDetailDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqKeywordDtlDetailDTO reqData = reqInfo.getReqData();
        ResKeywordDtlDetailDTO res = new ResKeywordDtlDetailDTO();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        ZlKeywordDtl view = keywordDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(view, res);
        return res;
    }
    
    @Override
    public ResKeywordDtlAddDTO insertKeywordDtlInfo(ReqDTO<ReqKeywordDtlAddDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqKeywordDtlAddDTO reqData = reqInfo.getReqData();
        ResKeywordDtlAddDTO res = new ResKeywordDtlAddDTO();
        Date time = getSystemTime().getDate();
        
        ZlKeywordDtl record = new ZlKeywordDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initCreateInfo(record, reqInfo, time);
        record.setKeywordDtlId(IdUtil.simpleUUID());
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("keywordDtlId",record.getKeywordDtlId());
        //检查主键是否冲突
        if(keywordDtlMapper.countExistKey(paramMap)>0){
            throw new BizException(GlobalRespEnum.DUPLICATE_PRIMARY_KEY);
        }
        record.setKeywordVal(record.getKeywordVal().toLowerCase());
        keywordDtlMapper.insertSelective(record);
        
        ZlKeywordDtl keyInfo = keywordDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResKeywordDtlEditDTO updateKeywordDtlInfo(ReqDTO<ReqKeywordDtlEditDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqKeywordDtlEditDTO reqData = reqInfo.getReqData();
        ResKeywordDtlEditDTO res = new ResKeywordDtlEditDTO();
        Date time = getSystemTime().getDate();
        
        ZlKeywordDtl record = new ZlKeywordDtl();
        BeanUtil.copyProperties(reqData, record);
        
        initUpdateInfo(record, reqInfo, time);
        record.setKeywordVal(record.getKeywordVal().toLowerCase());
        keywordDtlMapper.updateByPrimaryKeySelective(record);
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("keywordDtlId",record.getKeywordDtlId());
        ZlKeywordDtl keyInfo = keywordDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public ResKeywordDtlDelDTO deleteKeywordDtlInfo(ReqDTO<ReqKeywordDtlDelDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqKeywordDtlDelDTO reqData = reqInfo.getReqData();
        ResKeywordDtlDelDTO res = new ResKeywordDtlDelDTO();
        Date time = getSystemTime().getDate();
        
        Map<String,Object> paramMap = new HashMap<>();
        paramMap = BeanUtil.beanToMap(reqData);
        keywordDtlMapper.deleteByMap(paramMap);
        
        return res;
    }
    
    @Override
    public ResKeywordDtlStateDTO updateKeywordDtlState(ReqDTO<ReqKeywordDtlStateDTO> reqInfo) throws Exception{
        // TODO Auto-generated method stub
        ReqKeywordDtlStateDTO reqData = reqInfo.getReqData();
        ResKeywordDtlStateDTO res = new ResKeywordDtlStateDTO();
        Date time = getSystemTime().getDate();
         
        ZlKeywordDtl record = new ZlKeywordDtl();
        BeanUtil.copyProperties(reqData, record);
        initUpdateInfo(record, reqInfo, time);
        keywordDtlMapper.updateByPrimaryKeySelective(record);
         
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("keywordDtlId",record.getKeywordDtlId());
        ZlKeywordDtl keyInfo = keywordDtlMapper.selectOneByMap(paramMap);
        BeanUtil.copyProperties(keyInfo, res);
        
        return res;
    }
    
    @Override
    public void doUploadKeywordDtl(String keywordId, List<String> keys) throws Exception{
        if(keys==null || keys.size()==0){
            return;
        }
        Map<String,Object> paramMap = new HashMap<>();
        paramMap.put("keywordId",keywordId);
        ZlKeyword keyword = keywordMapper.selectOneByMap(paramMap);
        keywordDtlMapper.deleteByMap(paramMap);
        ReqDTO<Object> reqInfo=new ReqDTO<Object>();
        reqInfo.setReqUserId(keyword.getCreateOprid());
        reqInfo.setReqUserName(keyword.getCreateName());
        for(String keywordVal:keys){
            Date time = getSystemTime().getDate();
            ZlKeywordDtl record = new ZlKeywordDtl();
            initCreateInfo(record, reqInfo, time);
            record.setKeywordDtlId(IdUtil.simpleUUID());
            record.setKeywordId(keywordId);
            record.setKeywordVal(keywordVal.toLowerCase());
            record.setOrderNum(0l);
            keywordDtlMapper.insertSelective(record);
        }
    }
}